//
//  DetailOfUserViewController.m
//  LoveOfPet
//
//  Created by 芦彬 on 15/11/18.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "DetailOfUserViewController.h"
#import "LuBDefine.h"
#import "LBAFNetworking.h"
#import "DetailOfUser.h"
#import "DetailOfUserTableViewCell.h"
#import "DetailOfUserView.h"
#import "DetailOfPhotographViewController.h"
#import <MJRefresh.h>

@interface DetailOfUserViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain)UITableView *tableView;

@property (nonatomic, retain)NSMutableArray *detailOfUserArray;

@property (nonatomic, assign)NSInteger size;

@property (nonatomic, assign)BOOL isUpLoading;

@end

@implementation DetailOfUserViewController

- (void)dealloc{
    [_ppid release];
    [_disname release];
    [_detailOfCricle release];
    [_detailOfUserArray release];
    [_tableView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.detailOfUserArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forBarMetrics:UIBarMetricsCompact];
    
    [self createDetailOfUserViewController];
    // Do any additional setup after loading the view.
}

- (void)downloadData:(NSInteger)size{
    NSString *ppid = self.detailOfCricle.ppid;
    NSString *url =[NSString stringWithFormat:@"http://api.epetbar.com/gutouv2/photo.html?page=%ld&do=commentslist&pagepcid=&ppid=%@&quick_pcid=&limit=10&develop=0",self.size, ppid];
    [LBNetWork handleGETWithUrlString:url parameters:nil showHuD:YES onView:nil successfulBlock:^(id responseObject) {
        NSArray *detailOfUserArr = [[responseObject objectForKey:@"data"] objectForKey:@"list"];
        if (!self.isUpLoading) {
            [self.detailOfUserArray removeAllObjects];
        }
        for (NSDictionary *dic in detailOfUserArr) {
            DetailOfUser *detailOfUser = [DetailOfUser detailOfUserWithDictionary:dic];
            [self.detailOfUserArray addObject:detailOfUser];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        if (detailOfUserArr.count == 0) {
            [self.tableView.footer endRefreshingWithNoMoreData];
        }
    } failureBlock:^(NSError *error) {
        NSLog(@"失败");
    }];
}

- (void)createDetailOfUserViewController{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.navigationItem.title = self.detailOfCricle.disname;
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CWIDTH, CHEIGHT - 64) style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self addHeaderRefresh];
    [self addFooterUploading];
}

- (void) addHeaderRefresh{
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.size = 1;
        self.isUpLoading = NO;
        [self downloadData:self.size];
    }];
    [self.tableView.header beginRefreshing];
}

- (void) addFooterUploading{
    self.tableView.footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        self.size += 1;
        self.isUpLoading = YES;
        [self downloadData:self.size];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.detailOfUserArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//总section的头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
//    if (self.detailOfUserArray.count == 0) {
//        return 0;
//    }else{
        DetailOfUserView *userView = [[DetailOfUserView alloc]initWithFrame:CGRectMake(0, 0, CWIDTH, 140)];
        userView.detailOfCircle = self.detailOfCricle;
        userView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self.tableView.tableHeaderView addSubview:userView];
        return [userView autorelease];
//    }
}

//头视图上固定值加上照片描述信息自适应高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
    CGFloat autoHeight = [DetailOfUserView heightOfLabel:self.detailOfCricle.photo_des andTextFont:[UIFont systemFontOfSize: 12] withLabelWidths:CWIDTH - 40];
    return 50 + CWIDTH / 6 + autoHeight;
}

//评论自适应高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat autoHeight = [DetailOfUserTableViewCell heightOfLabel:[self.detailOfUserArray[indexPath.row] content] andTextFont:[UIFont systemFontOfSize:12] withLabelWidths:CWIDTH - 10 * 2 -CWIDTH / 8];
    return 30 + CWIDTH / 8 + autoHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailOfUserTableViewCell *detailOfUserCell = [DetailOfUserTableViewCell detailOfUserTabelViewCellWithTableView:tableView];
    detailOfUserCell.detailOfUser = self.detailOfUserArray[indexPath.row];
    return detailOfUserCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    DetailOfPhotographViewController *detailOfPhotograpViewController = [[DetailOfPhotographViewController alloc]init];

    detailOfPhotograpViewController.uid = [self.detailOfUserArray[indexPath.row] pub_uid];
    detailOfPhotograpViewController.petname = [self.detailOfUserArray[indexPath.row] nickname];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailOfPhotograpViewController animated:YES];
    [detailOfPhotograpViewController release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
